listview: Revise constructors
authorMatthias Clasen <mclasen@redhat.com>
Sun, 26 Jul 2020 22:27:23 +0000 (18:27 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Sun, 26 Jul 2020 22:27:23 +0000 (18:27 -0400)
Make both gtk_list_view_new and gtk_list_view_new_with_factory
take a model as first argument, and make all arguments
allow-none and transfer full.

Update all callers.

demos/gtk-demo/listview_applauncher.c
demos/gtk-demo/listview_weather.c
demos/gtk-demo/listview_words.c
gtk/gtkcolumnview.c
gtk/gtkcustompaperunixdialog.c
gtk/gtklistview.c
gtk/gtklistview.h
tests/testcolumnview.c
tests/testlistdnd.c
tests/testlistview-animating.c
tests/testlistview.c

index a6eb2f98632aebf78da5418eb20d1287a44b56bd..92e46316890439257b60cf04894f0149ed1197b2 100644 (file)
@@ -166,14 +166,6 @@ do_listview_applauncher (GtkWidget *do_widget)
       g_signal_connect (factory, "setup", G_CALLBACK (setup_listitem_cb), NULL);
       g_signal_connect (factory, "bind", G_CALLBACK (bind_listitem_cb), NULL);
 
-      /* Create the list widget here.
-       */
-      list = gtk_list_view_new_with_factory (factory);
-      /* We connect the activate signal here. It's the function we defined
-       * above for launching the selected application.
-       */
-      g_signal_connect (list, "activate", G_CALLBACK (activate_cb), NULL);
-
       /* And of course we need to set the data model. Here we call the function
        * we wrote above that gives us the list of applications. Then we set
        * it on the list widget.
@@ -181,8 +173,15 @@ do_listview_applauncher (GtkWidget *do_widget)
        * to create as many listitems as it needs to show itself to the user.
        */
       model = create_application_list ();
-      gtk_list_view_set_model (GTK_LIST_VIEW (list), model);
-      g_object_unref (model);
+
+      /* Create the list widget here.
+       */
+      list = gtk_list_view_new_with_factory (model, factory);
+
+      /* We connect the activate signal here. It's the function we defined
+       * above for launching the selected application.
+       */
+      g_signal_connect (list, "activate", G_CALLBACK (activate_cb), NULL);
 
       /* List widgets should always be contained in a #GtkScrolledWindow,
        * because otherwise they might get too large or they might not
index f17ff0892103279a31345474f7836a10685c55b8..23b83906cb70493321daec54180852588b968d64 100644 (file)
@@ -281,18 +281,16 @@ GtkWidget *
 create_weather_view (void)
 {
   GtkWidget *listview;
-  GListModel *selection;
+  GListModel *model;
   GtkListItemFactory *factory;
 
   factory = gtk_signal_list_item_factory_new ();
   g_signal_connect (factory, "setup", G_CALLBACK (setup_widget), NULL);
   g_signal_connect (factory, "bind", G_CALLBACK (bind_widget), NULL);
-  listview = gtk_list_view_new_with_factory (factory);
+  model = G_LIST_MODEL (gtk_no_selection_new (create_weather_model ()));
+  listview = gtk_list_view_new_with_factory (model, factory);
   gtk_orientable_set_orientation (GTK_ORIENTABLE (listview), GTK_ORIENTATION_HORIZONTAL);
   gtk_list_view_set_show_separators (GTK_LIST_VIEW (listview), TRUE);
-  selection = G_LIST_MODEL (gtk_no_selection_new (create_weather_model ()));
-  gtk_list_view_set_model (GTK_LIST_VIEW (listview), selection);
-  g_object_unref (selection);
 
   return listview;
 }
index 4545ed48aa24a5bad95b703befd86d71c504af0c..50e40adc20299c62a17d337d69e76c48843b95a0 100644 (file)
@@ -157,7 +157,6 @@ do_listview_words (GtkWidget *do_widget)
     {
       GtkWidget *header, *listview, *sw, *vbox, *search_entry, *open_button, *overlay;
       GtkFilterListModel *filter_model;
-      GtkNoSelection *selection;
       GtkStringList *stringlist;
       GtkFilter *filter;
       GFile *file;
@@ -215,12 +214,10 @@ do_listview_words (GtkWidget *do_widget)
       gtk_overlay_set_child (GTK_OVERLAY (overlay), sw);
 
       listview = gtk_list_view_new_with_factory (
+          G_LIST_MODEL (gtk_no_selection_new (G_LIST_MODEL (filter_model))),
           gtk_builder_list_item_factory_new_from_bytes (NULL,
               g_bytes_new_static (factory_text, strlen (factory_text))));
       gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), listview);
-      selection = gtk_no_selection_new (G_LIST_MODEL (filter_model));
-      gtk_list_view_set_model (GTK_LIST_VIEW (listview), G_LIST_MODEL (selection));
-      g_object_unref (selection);
 
       g_signal_connect (filter_model, "items-changed", G_CALLBACK (update_title_cb), progress);
       g_signal_connect (filter_model, "notify::pending", G_CALLBACK (update_title_cb), progress);
index 691c18e6edcdb1e6bff731d3b7fd05031dd9a2d8..590c7073d3eefd5f9a3a1288ad5a11cfc01629b7 100644 (file)
@@ -1161,7 +1161,7 @@ gtk_column_view_init (GtkColumnView *self)
 
   self->sorter = gtk_column_view_sorter_new ();
   self->factory = gtk_column_list_item_factory_new (self);
-  self->listview = GTK_LIST_VIEW (gtk_list_view_new_with_factory (
+  self->listview = GTK_LIST_VIEW (gtk_list_view_new_with_factory (NULL,
         GTK_LIST_ITEM_FACTORY (g_object_ref (self->factory))));
   gtk_widget_set_hexpand (GTK_WIDGET (self->listview), TRUE);
   gtk_widget_set_vexpand (GTK_WIDGET (self->listview), TRUE);
index 138f4e4ef1728a7b53690d41ad7c31d0d4918b15..ef2cbeadd24e34110c5f0542f4fb6e0259b6fdf6 100644 (file)
@@ -893,20 +893,16 @@ populate_dialog (GtkCustomPaperUnixDialog *dialog)
   gtk_box_append (GTK_BOX (vbox), scrolled);
   gtk_widget_show (scrolled);
 
-  listview = gtk_list_view_new ();
-  gtk_widget_set_size_request (listview, 140, -1);
-
   model = G_LIST_MODEL (gtk_single_selection_new (g_object_ref (G_LIST_MODEL (dialog->custom_paper_list))));
-  gtk_list_view_set_model (GTK_LIST_VIEW (listview), model);
   g_signal_connect (model, "notify::selected", G_CALLBACK (selected_custom_paper_changed), dialog);
-  g_object_unref (model);
 
   factory = gtk_signal_list_item_factory_new ();
   g_signal_connect (factory, "setup", G_CALLBACK (setup_item), NULL);
   g_signal_connect (factory, "bind", G_CALLBACK (bind_item), NULL);
   g_signal_connect (factory, "unbind", G_CALLBACK (unbind_item), NULL);
-  gtk_list_view_set_factory (GTK_LIST_VIEW (listview), factory);
-  g_object_unref (factory);
+
+  listview = gtk_list_view_new_with_factory (model, factory);
+  gtk_widget_set_size_request (listview, 140, -1);
 
   dialog->listview = listview;
 
index 3a65706ee6a69abf6b8ee13a530f4a1e988546b8..e0a33b42f4beefc0a44b36d6a7cac455ebf26a78 100644 (file)
  *
  * ...
  *
+ *   model = create_application_list ();
+ *
  *   factory = gtk_signal_list_item_factory_new ();
  *   g_signal_connect (factory, "setup", G_CALLBACK (setup_listitem_cb), NULL);
  *   g_signal_connect (factory, "bind", G_CALLBACK (bind_listitem_cb), NULL);
  *
- *   list = gtk_list_view_new_with_factory (factory);
+ *   list = gtk_list_view_new_with_factory (model, factory);
  *
  *   g_signal_connect (list, "activate", G_CALLBACK (activate_cb), NULL);
  *
- *   model = create_application_list ();
- *   gtk_list_view_set_model (GTK_LIST_VIEW (list), model);
- *   g_object_unref (model);
- *
  *   gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), list);
  * ]|
  *
@@ -931,52 +929,66 @@ gtk_list_view_init (GtkListView *self)
 
 /**
  * gtk_list_view_new:
+ * @model: (allow-none) (transfer full): the model to use, or %NULL
  *
- * Creates a new empty #GtkListView.
+ * Creates a new #GtkListView.
  *
- * You most likely want to call gtk_list_view_set_factory() to
- * set up a way to map its items to widgets and gtk_list_view_set_model()
- * to set a model to provide items next.
+ * You most likely want to call gtk_list_view_set_factory()
+ * to set up a way to map its items to widgets.
  *
  * Returns: a new #GtkListView
  **/
 GtkWidget *
-gtk_list_view_new (void)
+gtk_list_view_new (GListModel *model)
 {
-  return g_object_new (GTK_TYPE_LIST_VIEW, NULL);
+  GtkWidget *result;
+
+  g_return_val_if_fail (model == NULL || G_IS_LIST_MODEL (model), NULL);
+
+  result = g_object_new (GTK_TYPE_LIST_VIEW,
+                         "model", model,
+                         NULL);
+
+  /* consume the reference */
+  g_clear_object (&model);
+
+  return result;
 }
 
 /**
  * gtk_list_view_new_with_factory:
- * @factory: (transfer full): The factory to populate items with
+ * @model: (allow-none) (transfer full): the model to use, or %NULL
+ * @factory: (allow-none) (transfer full): The factory to populate items with, or %NULL
  *
  * Creates a new #GtkListView that uses the given @factory for
  * mapping items to widgets.
  *
- * You most likely want to call gtk_list_view_set_model() to set
- * a model next.
- *
  * The function takes ownership of the
  * argument, so you can write code like
  * ```
- *   list_view = gtk_list_view_new_with_factory (
- *     gtk_builder_list_item_factory_newfrom_resource ("/resource.ui"));
+ *   list_view = gtk_list_view_new_with_factory (create_model (),
+ *     gtk_builder_list_item_factory_new_from_resource ("/resource.ui"));
  * ```
  *
  * Returns: a new #GtkListView using the given @factory
  **/
 GtkWidget *
-gtk_list_view_new_with_factory (GtkListItemFactory *factory)
+gtk_list_view_new_with_factory (GListModel         *model,
+                                GtkListItemFactory *factory)
 {
   GtkWidget *result;
 
-  g_return_val_if_fail (GTK_IS_LIST_ITEM_FACTORY (factory), NULL);
+  g_return_val_if_fail (model == NULL || G_IS_LIST_MODEL (model), NULL);
+  g_return_val_if_fail (factory == NULL || GTK_IS_LIST_ITEM_FACTORY (factory), NULL);
 
   result = g_object_new (GTK_TYPE_LIST_VIEW,
+                         "model", model,
                          "factory", factory,
                          NULL);
 
-  g_object_unref (factory);
+  /* consume the references */
+  g_clear_object (&model);
+  g_clear_object (&factory);
 
   return result;
 }
index afcfb6225fec0e2e64928ce698e1335954c9669c..e499cfb9d377c60518bd0e086862b75309600a31 100644 (file)
@@ -47,9 +47,10 @@ GDK_AVAILABLE_IN_ALL
 GType           gtk_list_view_get_type                          (void) G_GNUC_CONST;
 
 GDK_AVAILABLE_IN_ALL
-GtkWidget *     gtk_list_view_new                               (void);
+GtkWidget *     gtk_list_view_new                               (GListModel             *model);
 GDK_AVAILABLE_IN_ALL
-GtkWidget *     gtk_list_view_new_with_factory                  (GtkListItemFactory     *factory);
+GtkWidget *     gtk_list_view_new_with_factory                  (GListModel             *model,
+                                                                 GtkListItemFactory     *factory);
 
 GDK_AVAILABLE_IN_ALL
 GListModel *    gtk_list_view_get_model                         (GtkListView            *self);
index e60eab674693fcfdc931a2c03d112969889ee195..58638b9adf04f8ba0a6bd6dff8ad16c87a54ab4f 100644 (file)
@@ -771,8 +771,8 @@ main (int argc, char *argv[])
   g_object_unref (filter);
 
   list = gtk_list_view_new_with_factory (
+             g_object_ref (gtk_column_view_get_columns (GTK_COLUMN_VIEW (view))),
              gtk_builder_list_item_factory_new_from_bytes (scope, g_bytes_new_static (factory_ui, strlen (factory_ui))));
-  gtk_list_view_set_model (GTK_LIST_VIEW (list), gtk_column_view_get_columns (GTK_COLUMN_VIEW (view)));
   gtk_box_append (GTK_BOX (hbox), list);
 
   g_object_unref (scope);
@@ -783,6 +783,5 @@ main (int argc, char *argv[])
   while (g_list_model_get_n_items (toplevels))
     g_main_context_iteration (NULL, TRUE);
 
-
   return 0;
 }
index 533f0a8d380a9903099cda2b1fda06c7baaf828a..338b5228421cf728acb526933c0a60d3a213271f 100644 (file)
@@ -365,13 +365,9 @@ main (int argc, char *argv[])
   gtk_scrolled_window_set_has_frame (GTK_SCROLLED_WINDOW (sw), TRUE);
   gtk_stack_add_titled (GTK_STACK (stack), sw, "list", "GtkListView");
 
-  list = gtk_list_view_new ();
+  list = gtk_list_view_new (create_model (0, 400, 1, FALSE));
   gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), list);
 
-  model = create_model (0, 400, 1, FALSE);
-  gtk_list_view_set_model (GTK_LIST_VIEW (list), model);
-  g_object_unref (model);
-
   factory = gtk_signal_list_item_factory_new ();
   g_signal_connect (factory, "setup", G_CALLBACK (setup_item), NULL);
   g_signal_connect (factory, "bind", G_CALLBACK (bind_item), NULL);
@@ -415,13 +411,9 @@ main (int argc, char *argv[])
   gtk_scrolled_window_set_has_frame (GTK_SCROLLED_WINDOW (sw), TRUE);
   gtk_stack_add_titled (GTK_STACK (stack), sw, "tree", "Tree");
 
-  list = gtk_list_view_new ();
+  list = gtk_list_view_new (create_tree_model (20, 20));
   gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), list);
 
-  model = create_tree_model (20, 20);
-  gtk_list_view_set_model (GTK_LIST_VIEW (list), model);
-  g_object_unref (model);
-
   factory = gtk_signal_list_item_factory_new ();
   g_signal_connect (factory, "setup", G_CALLBACK (setup_tree_item), NULL);
   g_signal_connect (factory, "bind", G_CALLBACK (bind_tree_item), NULL);
index 7902948ab25e110abb134c73871fc5384a8a9923..1baac2a7b98ef9735db2d22f6e9d37bdfe3b5166 100644 (file)
@@ -148,7 +148,7 @@ main (int   argc,
   factory = gtk_signal_list_item_factory_new ();
   g_signal_connect (factory, "setup", G_CALLBACK (setup_list_item), NULL);
   g_signal_connect (factory, "bind", G_CALLBACK (bind_list_item), NULL);
-  listview = gtk_list_view_new_with_factory (factory);
+  listview = gtk_list_view_new_with_factory (NULL, factory);
 
   gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), listview);
 
index c0552d14af105891cb6b4ae11d94731366e78d57..567ab3224c6cff242fdfc4230e5383d3c45fcee9 100644 (file)
@@ -615,7 +615,7 @@ main (int argc, char *argv[])
 
   factory = gtk_signal_list_item_factory_new ();
   g_signal_connect (factory, "setup", G_CALLBACK (setup_widget), NULL);
-  listview = gtk_list_view_new_with_factory (factory);
+  listview = gtk_list_view_new_with_factory (NULL, factory);
   gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), listview);
 
   if (argc > 1)